package com.xyht.sca_s.student_manage_system.modules.carUseReturn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.CacheUtil;
import com.xyht.sca_s.student_manage_system.modules.carUseReturn.entity.Resp.SmsCarUseReturnApplicationProcessResp;
import com.xyht.sca_s.student_manage_system.modules.carUseReturn.entity.SmsCarUseReturnApplicationProcess;
import com.xyht.sca_s.student_manage_system.modules.carUseReturn.entity.req.SmsCarUseReturnApplicationProcessReq;
import com.xyht.sca_s.student_manage_system.modules.carUseReturn.mapper.SmsCarUseReturnApplicationProcessMapper;
import com.xyht.sca_s.student_manage_system.modules.carUseReturn.service.SmsCarUseReturnApplicationProcessService;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsRole;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.entity.resp.SmsSearchUserResp;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsRoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.carUseManage.constant.carUseObjectTypeConstant.CAR_USE_OBJECT_TYPE_PERSON;
import static com.xyht.sca_s.student_manage_system.modules.carUseManage.constant.carUseObjectTypeConstant.CAR_USE_OBJECT_TYPE_ROLE;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-08-02
 */
@Service
public class SmsCarUseReturnApplicationProcessServiceImpl extends ServiceImpl<SmsCarUseReturnApplicationProcessMapper, SmsCarUseReturnApplicationProcess> implements SmsCarUseReturnApplicationProcessService {
    @Autowired
    private SmsCarUseReturnApplicationProcessMapper smsCarUseReturnApplicationProcessMapper;
    @Autowired
    private CacheUtil cacheUtil;
    @Autowired
    private SmsRoleMapper smsRoleMapper;
    @Override
    public ResponseResult updateProcess(SmsCarUseReturnApplicationProcessReq processVO) {
        if (isNullOrEmpty(processVO.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        if (isNullOrEmpty(processVO.getProcessObjectType())) {//如果流程对象类型不是空

            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
//            }
        }

        SmsCarUseReturnApplicationProcess process =
                smsCarUseReturnApplicationProcessMapper.selectById(processVO.getId());
        if (process == null || isNullOrEmpty(process.getId())) {
            return CommonResult.failed(CommonCodeEnum.CAR_USE_PROCESS_NOT_EXIST);
        }

        if (processVO.getProcessObjectType() != null && !isNullOrEmpty(processVO.getProcessObjectId())) {
            if (processVO.getProcessObjectType() == CAR_USE_OBJECT_TYPE_PERSON
                    && !processVO.getProcessIsApplicationEdit() ) {
                SmsUser smsUser = cacheUtil.getUserInfo(processVO.getProcessObjectId());
                if (smsUser == null || isNullOrEmpty(smsUser.getId())) {
                    return CommonResult.failed(CommonCodeEnum.CAR_USE_PROCESS_USER_NOT_EXIST);
                }
            } else if (processVO.getProcessObjectType() == CAR_USE_OBJECT_TYPE_ROLE) {
                SmsRole smsRole = smsRoleMapper.selectById(processVO.getProcessObjectId());
                if (smsRole == null || isNullOrEmpty(smsRole.getId())) {
                    return CommonResult.failed(CommonCodeEnum.CAR_USE_PROCESS_ROLE_NOT_EXIST);
                }
            }
        }

        SmsCarUseReturnApplicationProcess updateProcess = new SmsCarUseReturnApplicationProcess();
        updateProcess.setId(processVO.getId());
        if (!isNullOrEmpty(processVO.getProcessObjectType())) {
            updateProcess.setProcessObjectId(processVO.getProcessObjectId());
            updateProcess.setProcessObjectType(processVO.getProcessObjectType());
        }
        if (!isNullOrEmpty(processVO.getProcessIsApplicationEdit())) {
            updateProcess.setProcessIsApplicationEdit(processVO.getProcessIsApplicationEdit());
        }
        if (!isNullOrEmpty(processVO.getProcessIsSign())) {
            updateProcess.setProcessIsSign(processVO.getProcessIsSign());
        }
        if (!isNullOrEmpty(processVO.getProcessIsStamp())) {
            updateProcess.setProcessIsStamp(processVO.getProcessIsStamp());
        }
        if (!isNullOrEmpty(processVO.getProcessIsUse())) {
            updateProcess.setProcessIsUse(processVO.getProcessIsUse());
        }

        smsCarUseReturnApplicationProcessMapper.updateById(updateProcess);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getProcessList() {
        List<SmsCarUseReturnApplicationProcessResp> respList =
                smsCarUseReturnApplicationProcessMapper.selectList(new QueryWrapper<SmsCarUseReturnApplicationProcess>()
                        .lambda()
                        .orderByAsc(SmsCarUseReturnApplicationProcess::getProcessSort))
                        .stream()
                        .map(process -> {
                            SmsCarUseReturnApplicationProcessResp processResp =
                                    new SmsCarUseReturnApplicationProcessResp();
                            BeanUtils.copyProperties(process, processResp);

                            if (process.getProcessObjectType() != null && !isNullOrEmpty(process.getProcessObjectId())) {
                                if (process.getProcessObjectType() == CAR_USE_OBJECT_TYPE_PERSON) {
                                    SmsUser smsUser = cacheUtil.getUserInfo(process.getProcessObjectId());
                                    if (smsUser != null && !isNullOrEmpty(smsUser.getId())) {
                                        SmsSearchUserResp userResp = new SmsSearchUserResp();
                                        BeanUtils.copyProperties(smsUser, userResp);
                                        processResp.setProcessObjectName(smsUser.getRealName());
                                        processResp.setUserInfo(userResp);
                                    }
                                } else {
                                    SmsRole smsRole = smsRoleMapper.selectById(process.getProcessObjectId());
                                    if (smsRole != null && !isNullOrEmpty(smsRole.getId())) {
                                        processResp.setProcessObjectName(smsRole.getRoleName());
                                    }
                                }
                            }
                            return processResp;
                        })
                        .collect(Collectors.toList());
        return CommonResult.success(respList);
    }
}
